package com.hnkywl.sts.service.xtgl;

import com.hnkywl.sts.dao.xtgl.DlrzDao;
import com.hnkywl.sts.entity.xtgl.Dlrz;
import com.hnkywl.sts.listener.UserInfo;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-8
 * Time: 下午4:07
 * To change this template use File | Settings | File Templates.
 */
@Service
public class DlrzService {
    private DlrzDao dlrzDao;
    private CzrzService czrzService;

    private YgService ygService;

    //登录日志查询显示
    public String findDlrzList(Long dlrId, String dlrXm, Date dlrq, int pageNo, int pageSize) {
        Page<Dlrz> dlrzPage = this.dlrzDao.findDlrzList(dlrId, dlrXm, dlrq, pageNo, pageSize);
        List<Dlrz> dlrzList = dlrzPage.getResult() == null ? new ArrayList<Dlrz>() : dlrzPage.getResult();
        String dlrzJson = BaseUtils.fetchCollectionToJson(dlrzList, "id", "dlr.id", "dlr.xm", "dlr.zgjg.qc", "dlrq", "dlip","dlmac", "zt");
        return "{\"total\":" + dlrzPage.getTotalCount() + ",\"rows\":[" + dlrzJson + "]}";
    }

    //用户登录
    @Transactional
    public void userLogin(UserInfo userInfo) {
        Dlrz dlrz = new Dlrz();
        dlrz.setDlip(userInfo.getRemoteIP());
        dlrz.setDlmac(userInfo.getMacAddress());
        dlrz.setDlrq(new Date());
        dlrz.setDlr(ygService.findYgDetail(userInfo.getUserId()));
        dlrz.setZt(1);
        this.dlrzDao.save(dlrz);

        //保存一下操作日志
        this.czrzService.loginLog(dlrz);
    }

    //用户登出
    @Transactional
    public void userLogout(Long ygid, String remortIP, String macAddress) {
        Dlrz dlrz = new Dlrz();
        dlrz.setDlip(remortIP);
        dlrz.setDlmac(macAddress);
        dlrz.setDlrq(new Date());
        dlrz.setDlr(ygService.findYgDetail(ygid));
        dlrz.setZt(2);
        this.dlrzDao.save(dlrz);

        //保存一下操作日志
        this.czrzService.logoutLog(dlrz);
    }

    @Autowired
    public void setDlrzDao(DlrzDao dlrzDao) {
        this.dlrzDao = dlrzDao;
    }

    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }

    @Autowired
    public void setCzrzService(CzrzService czrzService) {
        this.czrzService = czrzService;
    }
}
